<?php
/**
 * Created by PhpStorm.
 * User: 86131
 * Date: 2020/10/22
 * Time: 11:03
 */
namespace apiadmin\modules\v1\controllers\finance;
use apiadmin\modules\models\admin\User;
use apiadmin\modules\v1\controllers\CoreController;
use common\models\BalancePriceLog;
use common\models\BalanceType;
use common\models\Setting;
use Yii;

class SettingController extends CoreController
{
    public function actionExchangeSwitch()
    {
        $data = Setting::getOne(['type' => 'exchange_switch', 'code' => 'exchange_switch']);
        $this->out('兑换开关', $data);
    }

    public function actionEditExchangeSwitch()
    {
        $auth_key = $this->request('auth_key');
        $user_model = User::find()->where(['auth_key' => $auth_key])->asarray()->one();
        if (!$user_model) {
            if (!$user_model) $this->error('参数有误');
        }
        $exchange_switch = intval($this->request('state')) + 0;
        if (!$exchange_switch || $exchange_switch < 0 || $exchange_switch>2)
            $this->error('参数有误');
        $model = Setting::find()->where(['type' => 'exchange_switch', 'code' => 'exchange_switch'])->one();
        $params = [];
        $params['value'] = '' . $exchange_switch;
        if (!$model) {
            $model = new Setting;
            $params['type'] = 'exchange_switch';
            $params['code'] = 'exchange_switch';
            $params['name'] = '兑换UPH审核开关';
            $params['describe'] = '2不需要审核1需要审核';
            $params['is_edit'] = 1;
//            $params['create_time'] = date("Y-m-d H:i:s");
        }
        if (!$model->load($params, '') || !$model->validate() || !$model->save(false)) {
            $this->error($model::outError($model->getErrors()));
        }
        $this->out('操作成功');
    }

    public function actionPriceUph()
    {
        $data = BalanceType::getOne(['type' => '2']);
        $this->out('uph价格', $data);
    }

    public function actionEditPriceUph()
    {
        //参数校验
        $price = floatval($this->request('price'))+0;
        if(!$price||$price<0)
            $this->error('金额数量不能小于0');
        $model = BalanceType::find()->where(['type' => '2'])->one();
        $params = [];
        $params['price'] = ''.$price;
        if(!$model){
            $this->error('账本类型不正确');
        }

        if(!$model->load($params, '') || !$model->validate() || !$model->save(false)){
            $this->error($model::outError($model->getErrors()));
        }

        $model = BalancePriceLog::find()->where(['create_time' => date('Y-m-d')])->one();
        if(!$model){
            $model = new BalancePriceLog;
            $model->create_time = date('Y-m-d');
            $model->btype = 2;
        }
        $model->price = $params['price'];
        $model->save();
        $this->out('操作成功');
    }
}